package com.woniuxy01;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 作者
 * @create 2025-09-24 9:48
 */
public class Test01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Map<String,Object> map = new HashMap<>();
        map.put("sname","zs");
        map.put("tname","t001");
        map.put("sid",1);
        map.put("age",18);
        map.put("x","xxxxz");
        Student student = mapToBean(map, Student.class);
        System.out.println(student);

        Teacher teacher = mapToBean(map, Teacher.class);
        System.out.println(teacher);

    }

    //map转Bean,如何创建任意对象
    static <T> T  mapToBean(Map<String,Object> map,Class<T> clazz) throws InstantiationException, IllegalAccessException {
        T t = clazz.newInstance();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //如果map的key和对象字段一致
            if(map.containsKey(field.getName())){
                //将map对应key的value赋值给属性
                field.setAccessible(true);
                field.set(t,map.get(field.getName()));
            }
        }
        return t;
    }

    private static void test05() throws InstantiationException, IllegalAccessException {
        Map<String,Object> map = new HashMap<>();
        map.put("sname","zs");
        map.put("sid",1);
        map.put("age",18);
        map.put("x","xxxxz");

        //c创建对象
        Class clazz = Student.class;
        Student student = (Student) clazz.newInstance();
        //使用反射API，使用map中的key-value,给student对象的属性赋值
        //获取所有字段
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //如果map的key和对象字段一致
            if(map.containsKey(field.getName())){
                //将map对应key的value赋值给属性
                field.setAccessible(true);
                field.set(student,map.get(field.getName()));
            }
        }
        System.out.println(student);
    }

    private static void test04() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        //操作公有属性
        Student student = (Student) clazz.newInstance();
        //获取属性
        Field fieldEmail = clazz.getField("email");
        //给属性赋值
        fieldEmail.set(student,"zs@163.com");
        //获取属性值
        System.out.println(fieldEmail.get(student));

        //操作私有属性
        Field fieldSname = clazz.getDeclaredField("sname");
        //反射可以操作私有成员，设置暴力访问
        fieldSname.setAccessible(true);
        fieldSname.set(student,"zs");
        System.out.println(student);


//        Student student1 = new Student();
//        System.out.println(student1.email);
//        student1.email="zs";
    }

    private static void test03() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        Class clazz = Class.forName("com.woniuxy01.Student");
        Student student = (Student) clazz.newInstance();
        System.out.println("getFields()获取public属性");
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            System.out.println(field);
//            System.out.println(field.getName());
//            System.out.println(field.getType());
        }
        System.out.println("getFields()获取所有属性");
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println(field);
        }
        System.out.println("getField(String name)获取指定的公有字段");
        Field fieldEmail = clazz.getField("email");
        System.out.println(fieldEmail);
        System.out.println("getDeclaredField(String name)获取指定的字段");
        Field fieldAge = clazz.getDeclaredField("email");
        System.out.println(fieldAge);
    }

    private static void test02() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //获取反射字节码文件对象
        Class clazz = Class.forName("com.woniuxy01.Student");
        //使用反射创建对象
        Student student = (Student) clazz.newInstance();
        System.out.println(student);

//        Student student1 = new Student();
//        System.out.println(student1);
    }

    private static void test01() throws ClassNotFoundException {
        Student student = new Student();
        //获取Student类对应.class文件
        //对象.getClass()
        Class clazz1 = student.getClass();
        //类型.class，基本数据类型都可以
        Class clazz2 = Student.class;
        //Class.forName("全限定类名")
        Class clazz3 = Class.forName("com.woniuxy01.Student");
        System.out.println(clazz1==clazz2);
        System.out.println(clazz1==clazz3);
    }
}
